//
//  WJBottomPopupView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/20.
//

import UIKit

class WJBottomPopupView: WJBasePopupView {
    
    private let titleLabel = UILabel()
    private let messageLabel = UILabel()
    private let confirmButton = UIButton()
    private let cancelButton = UIButton()
    
    var onConfirm: (() -> Void)?
    var onCancel: (() -> Void)?
    
    override func setupContent() {
        super.setupContent()
        setupContentView()
        setupConstraints()
    }
    
    private func setupContentView() {
        // 标题
        titleLabel.text = "提示"
        titleLabel.font = UIFont.boldSystemFont(ofSize: 18)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .black
        contentView.addSubview(titleLabel)
        
        // 消息
        messageLabel.text = "这是一个从底部弹出的提示框"
        messageLabel.font = UIFont.systemFont(ofSize: 16)
        messageLabel.textAlignment = .center
        messageLabel.textColor = .darkGray
        messageLabel.numberOfLines = 0
        contentView.addSubview(messageLabel)
        
        // 确认按钮
        confirmButton.setTitle("确认", for: .normal)
        confirmButton.backgroundColor = .systemBlue
        confirmButton.setTitleColor(.white, for: .normal)
        confirmButton.layer.cornerRadius = 8
        confirmButton.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
        contentView.addSubview(confirmButton)
        
        // 取消按钮
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.backgroundColor = .lightGray
        cancelButton.setTitleColor(.black, for: .normal)
        cancelButton.layer.cornerRadius = 8
        cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
        contentView.addSubview(cancelButton)
    }
    
    private func setupConstraints() {
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        confirmButton.translatesAutoresizingMaskIntoConstraints = false
        cancelButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            // 标题
            titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
            titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // 消息
            messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 15),
            messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            messageLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // 按钮
            confirmButton.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 25),
            confirmButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            confirmButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            confirmButton.heightAnchor.constraint(equalToConstant: 50),
            
            cancelButton.topAnchor.constraint(equalTo: confirmButton.bottomAnchor, constant: 10),
            cancelButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            cancelButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            cancelButton.heightAnchor.constraint(equalToConstant: 50),
            cancelButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20)
        ])
    }
    
    func configure(title: String, message: String, confirmText: String = "确认", cancelText: String = "取消") {
        titleLabel.text = title
        messageLabel.text = message
        confirmButton.setTitle(confirmText, for: .normal)
        cancelButton.setTitle(cancelText, for: .normal)
    }
    
    @objc private func confirmTapped() {
        dismiss {
            self.onConfirm?()
        }
    }
    
    @objc private func cancelTapped() {
        dismiss {
            self.onCancel?()
        }
    }
}
